﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Web;
using System.Globalization;
using System.IO;
using Eagle.Common;

namespace Eagle.Config
{
    /// <summary>
    /// Eagle自身所需参数配置类
    /// </summary>
    public class EagleSettings
    {
        #region 初始化实例
        private static EagleSettings _instance = null;
        private EagleSettings()
        {
            GetInstance();
        }
        /// <summary>
        /// EagleSettings实例
        /// </summary>
        public static EagleSettings Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new EagleSettings();
                return _instance;
            }
        }
        private void GetInstance()
        {
            string error = string.Empty;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                string filePath = HttpContext.Current.Server.MapPath("~/config/eaglesettings.config");
                if (!File.Exists(filePath))
                    ReponseAlertMessage("Eagle组件缺少必要的配置文件EagleSettings.config，请在网站根路径下添加目录config，并把EagleSettings.config文件拷贝进去");
                xmlDoc.Load(filePath);
                foreach (PropertyInfo property in this.GetType().GetProperties())
                {
                    if (property.CanWrite)
                    {
                          XmlNode node = xmlDoc.SelectSingleNode("/settings/" + property.Name);
                          if (node == null)
                          {
                                error += "，缺少节点"+property.Name;
                                continue;
                          }
                        string value = xmlDoc.SelectSingleNode("/settings/" + property.Name).InnerText;
                        property.SetValue(this, Convert.ChangeType(value, property.PropertyType, CultureInfo.CurrentCulture), null);
                    }
                }
            }
            catch (Exception e)
            {
                  error += "Eagle.Config配置出错，" + e.Message;

            }
            ReponseAlertMessage(error);
        }

        private static void ReponseAlertMessage(string error)
        {
            if (error.Length > 0)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Write("<script language=\"javascript\" type=\"text/javascript\">alert('" +  error.TrimStart('，') + "')</script>");
                HttpContext.Current.Response.End();
            }
        }
        #endregion

        #region Cookies的域设置
        /// <summary>
        /// Cookies的域
        /// </summary>
        public string CookiesDomain { get; set; }
        #endregion

        #region 邮件发送参数设置
        /// <summary>
        /// 发送邮箱的名称
        /// </summary>
        public string ServerEmailName { get; set; }
        /// <summary>
        /// 发送邮箱的密码
        /// </summary>
        public string ServerEmailPassword { get; set; }
        /// <summary>
        /// 邮箱服务器的SMTP
        /// </summary>
        public string ServerEmailSmtp { get; set; }
        #endregion

        #region 缓存配置
        /// <summary>
        /// 缓存使用类全名称（包括命名空间）
        /// </summary>
        public string CacheClassName { get; set; }
        #endregion

        #region 网站编码设置
        /// <summary>
        /// 网站编码
        /// </summary>
        public string Encoding { get; set; } 
        #endregion
    }
}
